package ee.ut.maxorato_macobo.oop.mudclient.ui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.java.dev.designgridlayout.DesignGridLayout;

import ee.ut.maxorato_macobo.oop.mudclient.ClientMain;
import ee.ut.maxorato_macobo.oop.mudclient.network.ClientIoManager;

public class ControlsPanel extends JPanel implements ComponentListener {
	private static final long serialVersionUID = 3254462720581014023L;
	private JToggleButton connect;
	private JButton north, south, east, west;
	private DesignGridLayout layout;
	int width;

	ControlsPanel(int width) {
		super();
		this.width = width;
		
		connect = new JToggleButton("Connect");
		connect.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				ButtonModel button = ((AbstractButton)e.getSource()).getModel();
				ClientMain.changeConnection(button.isSelected());
			}
		});
		north = controlButton("n", directionListener("north"));
		east  = controlButton("e", directionListener("east"));
		west  = controlButton("w", directionListener("west"));
		south = controlButton("s", directionListener("south"));
		JButton rename = controlButton("name", setTextAction("rename ", false));
		JButton players = controlButton("players", setTextAction("allplayers", true));
		JButton look = controlButton("look around", setTextAction("look", true));
		JButton attack = controlButton("atk", setTextAction("attack ", false));
		JButton spell = controlButton("blast", setTextAction("cast blast", true));
		JButton inventory = controlButton("inventory", setTextAction("inventory", true));
		
		layout = new DesignGridLayout(this);
		layout.margins(0.4, 0, 5, 0);
		layout.row().grid().add(connect);
		layout.emptyRow();
		layout.row().grid().add(rename).add(players);
		layout.row().grid().add(look);
		layout.row().grid().add(attack).add(spell);
		layout.row().grid().add(inventory);
		layout.row().grid().add(west).add(north).add(south).add(east);
	}
	
	private JButton controlButton(String label, ActionListener action) {
		JButton button = new JButton(label);
		button.addActionListener(action);
		button.setBorder(new LineBorder(Color.BLUE, 0));
		return button;
	}
	
	private ActionListener directionListener(final String direction) {
		return new ActionListener() {
			final String dir = direction;
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.out.println(dir);
				ClientIoManager.gotInput(dir + "\n");
				ClientIoManager.sendOutput(dir + "\n");
			}
		};
	}
	
	private ActionListener setTextAction(final String atext, final boolean asend) {
		return new ActionListener() {
			final String text = atext;
			final boolean send = asend;
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!send)
					ClientMain.getMainWindow().setText(text);
				else {
					ClientIoManager.gotInput(text + "\n");
					ClientIoManager.sendOutput(text + "\n");
				}
			}
		};
	}	
	
	public void setConnected(boolean connected) {
		connect.setSelected(connected);
	}

	@Override
	public void componentHidden(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentMoved(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentResized(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentShown(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

}
